package cn.edu.scau.cmi.zhangmingxing.composite.consistent;





import java.util.*;


import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class ConsistentComponentController {
	private TreeItem parentItem=null;
	private ConsistentComponent safeComponent=null;
	private ConsistentComposite safeComposite=null;
    @FXML
    private Button deleteButton;

    @FXML
    private AnchorPane rootPane;

    @FXML
    private Button teamButton;

    @FXML
    private Button personalButton;

    @FXML
    private StackPane stackPane;
    @FXML
    private TextField textField;
    
    public void begin(Stage primaryStage,Parent root) {
    	primaryStage.setTitle("ConsistentComponent");  //标题      
    	ConsistentComposite safeComposite=new ConsistentComposite();
    	safeComposite.setName("组织");
        TreeItem<ConsistentComponent> rootItem = new TreeItem<> (safeComposite);//item
        rootItem.setExpanded(true);//伸展
        TreeView<ConsistentComponent> tree = new TreeView<> (rootItem);//item框
        tree.setEditable(true);
        stackPane.getChildren().add(tree);
        tree.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>()
        {
        	
            public void handle(MouseEvent event)
            {
                Node node = event.getPickResult().getIntersectedNode();
     
                if (node instanceof Text || (node instanceof TreeCell && ((TreeCell) node).getText() != null)) {
                	parentItem=tree.getSelectionModel().getSelectedItem();
                    
                }
            }
        });
        

        primaryStage.setScene(new Scene(root));
        primaryStage.show();

    }
    @FXML
    void putOnDelete(ActionEvent event) {
    	
       	if(parentItem!=null) {
       		TreeItem grandfatherItem=null;
       		grandfatherItem=parentItem.getParent();
       		if(grandfatherItem!=null)
       			
       			grandfatherItem.getChildren().remove(parentItem);
       	}
        	
        
    }

    @FXML
    void putOnTeam(ActionEvent event) {
    	if(parentItem.getValue() instanceof ConsistentComposite) {
    		ConsistentComposite father=(ConsistentComposite)parentItem.getValue();
    		if(textField.getText()!=null) {
    			safeComposite=new ConsistentComposite();
    			safeComposite.setName(textField.getText());
        		TreeItem<ConsistentComponent> sonItem = new TreeItem<> (safeComposite);
        		father.addChild(safeComposite);
        		parentItem.getChildren().add(sonItem);
    		}
    	}
 
    }
    @FXML
    void putOnPersonal(ActionEvent event) {
    	if(parentItem.getValue() instanceof ConsistentComposite) {
    		ConsistentComposite father=(ConsistentComposite)parentItem.getValue();
    		if(textField.getText()!=null) {
    			ConsistentLeaf safeLeaf=new ConsistentLeaf();
    			safeLeaf.setName(textField.getText());
    			TreeItem<ConsistentComponent> sonItem = new TreeItem<> (safeLeaf);
    			father.addChild(safeLeaf);
    			parentItem.getChildren().add(sonItem);
    		}
    	}
    }
   /*
    private final class TextFieldTreeCellImpl extends TreeCell<String> {

        // 文本编辑
        private TextField textField;

        // 添加菜单
        private final ContextMenu addMenu = new ContextMenu();

        // 构造方法,如果对儿子单元有特殊的需求可以在构造方法里实现


        // 所有的TreeView的儿子控件都是支持Edit的,所有每次选中编辑的时候都会执行该方法
        @Override
        public void startEdit() {
            super.startEdit();

            if (textField == null) {
                // 每个单元会拥有一个文本框
                createTextField();
            }
            setText(null);
            setGraphic(textField);
            textField.selectAll();
        }

        @Override
        public void cancelEdit() {
            super.cancelEdit();
            // 清空编辑
            setText((String) getItem());
            setGraphic(getTreeItem().getGraphic());
        }

        @Override
        public void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);

            // 为空不处理
            if (empty) {
                setText(null);
                setGraphic(null);
            } else {
                // 如果正在编辑
                if (isEditing()) {
                    if (textField != null) {
                        // 设置为原来Item的内容
                        textField.setText(getString());
                    }
                    setText(null);
                    // 设置显示为文本内容
                    setGraphic(textField);
                } else {
                    setText(getString());
                    setGraphic(getTreeItem().getGraphic());
                    // 如果有儿子  并且 有父亲，则拥有添加顾客菜单栏
                    if (!getTreeItem().isLeaf() && getTreeItem().getParent() != null) {
                        setContextMenu(addMenu);
                    }else {
                        // 其他情况没有菜单栏
                        setContextMenu(null);
                    }
                }
            }
        }

        // 创建文本区，名字为该item名
        private void createTextField() {
            textField = new TextField(getString());

            // 键盘事件处理，松开键盘时
            textField.setOnKeyReleased((KeyEvent t) -> {
                if (t.getCode() == KeyCode.ENTER) {
                    // 输入回车时，单元编辑为文本框内容
                    commitEdit(textField.getText());
                } else if (t.getCode() == KeyCode.ESCAPE) {
                    // 输入退出键时,清空编辑
                    cancelEdit();
                }
            });
        }

        // 获取该Item的名字
        private String getString() {
            return getItem() == null ? "" : getItem().toString();
        }
    }
*/


}

